/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.sync.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.box.entity.Box;
import com.pitop.box.service.BoxSV;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.customer.entity.Customer;
import com.pitop.customer.service.CustomerSV;
import com.pitop.order.entity.Order;
import com.pitop.order.service.OrderSV;
import com.pitop.sync.entity.SyncDataTrace;
import com.pitop.sync.service.SyncDataTraceSV;
import com.pitop.test.entity.TestRecord;
import com.pitop.test.entity.TestUnit;
import com.pitop.test.service.TestRecordSV;
import com.pitop.test.service.TestUnitSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 同步数据 控制器
 *
 * @author pi-top
 */
@RestController
@RequestMapping("/syncDataTrace")
@Slf4j
@Api(value = "同步数据控制器", description = "同步数据控制器")
public class SyncDataTraceCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private SyncDataTraceSV syncDataTraceSV;

    @Resource
    private TestUnitSV testUnitSV;

    @Resource
    private TestRecordSV testRecordSV;

    @Resource
    private BoxSV boxSV;

    @Resource
    private CustomerSV customerSV;

    @Resource
    OrderSV orderSV;

    /**
     * 查询SyncDataTrace一个详情信息
     *
     * @param id            id
     * @param syncTraceCode 同步追踪编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询SyncDataTrace一个详情信息", notes = "查询SyncDataTrace一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "syncTraceCode", value = "同步追踪编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public SyncDataTrace load(java.lang.Long id, java.lang.String syncTraceCode) {
        if (id == null) {
            return null;
        }
        if (syncTraceCode == null) {
            return null;
        }
        SyncDataTrace syncDataTrace = syncDataTraceSV.load(id, syncTraceCode);
        log.info(JSON.toJSONString(syncDataTrace));
        return syncDataTrace;
    }


    /**
     * 根据条件id查询SyncDataTrace一个详情信息
     *
     * @param id id
     * @return BeanRet
     */
    @ApiOperation(value = "查询SyncDataTrace一个详情信息", notes = "查询SyncDataTrace一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public SyncDataTrace loadById(@PathVariable java.lang.Long id) {
        if (id == null) {
            return null;
        }
        SyncDataTrace syncDataTrace = syncDataTraceSV.loadById(id);
        log.info(JSON.toJSONString(syncDataTrace));
        return syncDataTrace;
    }

    /**
     * 根据条件syncTraceCode查询SyncDataTrace一个详情信息
     *
     * @param syncTraceCode 同步追踪编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询SyncDataTrace一个详情信息", notes = "查询SyncDataTrace一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "syncTraceCode", value = "同步追踪编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/syncTraceCode/{syncTraceCode}")
    @ResponseBody
    public SyncDataTrace loadBySyncTraceCode(@PathVariable java.lang.String syncTraceCode) {
        if (syncTraceCode == null) {
            return null;
        }
        SyncDataTrace syncDataTrace = syncDataTraceSV.loadBySyncTraceCode(syncTraceCode);
        log.info(JSON.toJSONString(syncDataTrace));
        return syncDataTrace;
    }


    /**
     * 查询SyncDataTrace信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询SyncDataTrace信息集合", notes = "查询SyncDataTrace信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "syncTraceCode", value = "同步追踪编码", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "数据", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query")
    })
    @PostMapping(value = "/list")
    @ResponseBody
    public List<SyncDataTrace> list(@RequestBody @ApiIgnore SyncDataTrace syncDataTrace, Integer curPage, Integer pageSize) {
        Page<SyncDataTrace> page = new Page<SyncDataTrace>(pageSize, curPage);
        List<SyncDataTrace> syncDataTraces = syncDataTraceSV.list(syncDataTrace, page.genRowStart(), page.getPageSize());
        int total = syncDataTraceSV.count(syncDataTrace);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return syncDataTraces;
    }


    /**
     * 查询SyncDataTrace信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询SyncDataTrace信息集合", notes = "查询SyncDataTrace信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "syncTraceCode", value = "同步追踪编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public List<SyncDataTrace> listByPk(java.lang.Long id, java.lang.String syncTraceCode, Integer curPage, Integer pageSize) {
        Page<SyncDataTrace> page = new Page<SyncDataTrace>(pageSize, curPage);
        List<SyncDataTrace> syncDataTraces = syncDataTraceSV.list(id, syncTraceCode, page.genRowStart(), page.getPageSize());
        int total = syncDataTraceSV.count(id, syncTraceCode);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return syncDataTraces;
    }

    /**
     * 统计SyncDataTrace信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计SyncDataTrace信息数量根据主键", notes = " 统计SyncDataTrace信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "syncTraceCode", value = "同步追踪编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(java.lang.Long id, java.lang.String syncTraceCode) {
        return syncDataTraceSV.count(id, syncTraceCode);
    }

    /**
     * 统计SyncDataTrace信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计SyncDataTrace信息数量", notes = "统计SyncDataTrace信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "syncTraceCode", value = "同步追踪编码", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "数据", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query")
    })
    @PostMapping(value = "/count")
    @ResponseBody
    public Integer count(@RequestBody SyncDataTrace syncDataTrace) {
        return syncDataTraceSV.count(syncDataTrace);
    }


    /**
     * 创建SyncDataTrace
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建SyncDataTrace", notes = "创建SyncDataTrace")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "数据", paramType = "query"),
    })
    @PostMapping("/build")
    @ResponseBody
    public SyncDataTrace build(@RequestBody @ApiIgnore SyncDataTrace syncDataTrace) {
        syncDataTraceSV.save(syncDataTrace);
        return syncDataTrace;
    }


    /**
     * 修改SyncDataTrace
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改SyncDataTrace", notes = "修改SyncDataTrace")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "syncTraceCode", value = "同步追踪编码", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "数据", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public SyncDataTrace modify(@RequestBody @ApiIgnore SyncDataTrace syncDataTrace) {
        syncDataTraceSV.modify(syncDataTrace);
        return syncDataTrace;
    }

    /**
     * 删除SyncDataTrace
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除SyncDataTrace", notes = "删除SyncDataTrace")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "syncTraceCode", value = "同步追踪编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(java.lang.Long id, java.lang.String syncTraceCode) {
        syncDataTraceSV.delete(id, syncTraceCode);
        return BeanRet.create(true, "删除SyncDataTrace成功");
    }


    /**
     * pps向pps-cloud同步测试结果数据
     *
     * @param testUnitList
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "upwardsSyncTestUnit 从下往上同步测试件数据", notes = "upwardsSyncTestUnit 从下往上同步测试件数据")
    @RequestMapping(value = "/upwardsSyncTestUnit", method = RequestMethod.POST)
    @ResponseBody
    public boolean upwardsSyncTestUnit(@RequestBody List<TestUnit> testUnitList) throws Exception {
        return testUnitSV.upwardsSyncTestUnit(testUnitList);
    }

    /**
     * pps向pps-cloud同步测试结果数据
     *
     * @param testRecordList
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "upwardsSyncTestRecord 从下往上同步数据", notes = "upwardsSyncTestRecord 从下往上同步数据")
    @RequestMapping(value = "/upwardsSyncTestRecord", method = RequestMethod.POST)
    @ResponseBody
    public boolean upwardsSyncTestRecord(@RequestBody List<TestRecord> testRecordList) throws Exception {
        return testRecordSV.upwardsSyncTestRecord(testRecordList);
    }

    /**
     * pps向pps-cloud同步箱库存数据
     *
     * @param boxList
     * @return boolean
     */
    @ApiOperation(value = "从下往上同步箱库存数据", notes = "从下往上同步箱库存数据")
    @PostMapping(value = "/upwardsSyncBox")
    @ResponseBody
    public boolean upwardsSyncBox(@RequestBody List<Box> boxList) {
        return boxSV.upwardsSyncBox(boxList);
    }

    /**
     * pps向pps-cloud同步用户数据
     *
     * @param customerList
     * @return boolean
     */
    @ApiOperation(value = "pps向pps-cloud同步用户数据", notes = "pps向pps-cloud同步用户数据")
    @PostMapping(value = "/upwardsSyncCustomer")
    @ResponseBody
    public boolean upwardsSyncCustomer(@RequestBody List<Customer> customerList) {
        return customerSV.upwardsSyncCustomer(customerList);
    }

    /**
     * pps向pps-cloud同步订单数据
     *
     * @param orderList
     * @return boolean
     */
    @ApiOperation(value = "pps向pps-cloud同步订单数据", notes = "pps向pps-cloud同步订单数据")
    @PostMapping(value = "/upwardsSyncOrder")
    @ResponseBody
    public boolean upwardsSyncOrder(@RequestBody List<Order> orderList) {
        return orderSV.upwardsSyncOrder(orderList);
    }

}
